<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class news_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    public function index()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '16';
        $cats = $this->module('news')->cats();
        $sql = "select * from " . table('news') . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size;
        $query = $this->db->query($sql);
        while ($row = $this->db->fetch_array($query)) {
            if (isset($cats[$row['cid']])) {
                $row['cats_cn'] = $cats[$row['cid']];
            }
            $row['addtime'] = _date($row['addtime']);
            $data[] = $row;
        }
        $this->assign('data', $data);
        $total = $this->db->get_one("select count(id) as total from " . table("news"));
        $this->assign('page', $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total'])));
        $this->display();
    }
    public function add()
    {
        $this->assign('cats', $this->func_cats_list());
        $this->display('news/edit');
    }
    public function update()
    {
        $id = intval($_GET['id']) ? $_GET['id'] : exit('ID is not null');
        $row = $this->db->get_one("select * from " . table('news') . " where id = " . $id);
        if ($row['id']) {
            $row['content'] = $this->module('album')->img_content($row['content']);
            $this->assign('row', $row);
        } else {
            showmsg('获取数据失败');
        }
        $this->assign('cats', $this->func_cats_list());
        $this->display('news/edit');
    }
    public function save()
    {
        if ($_POST) {
            $data['cid'] = P('cid', '分类不能为空');
            $data['title'] = P('title', '标题不能为空');
            $data['alias'] = P('alias');
            $content = P('content', '内容不能为空');
            $data['content'] = $this->module('album')->img_replace($content);
            $data['status'] = P('content', '', '1');
            $data['link_status'] = P('link_status', '', '0');
            $data['link'] = P('link', '');
            $data['status'] = P('status', '', '1');
            $data['addtime'] = time();
            $id = P('id', 0);
            if ($id) {
                $query = $this->db->update(table('news'), $data, 'id = ' . $id);
            } else {
                $query = $this->db->add(table('news'), $data);
            }
            if ($query) {
                right_json();
            } else {
                error_json('保存失败');
            }
        } else {
            error_json('操作错误');
        }
    }
    public function del()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : showmsg('参数错误');
        $query = $this->db->delete(table('news'), 'id = ' . $id);
        if ($query) {
            showmsg('删除成功');
        } else {
            showmsg('删除失败');
        }
    }
    public function func_cats_list()
    {
        $query = $this->db->query("select * from " . table('news_cats') . " where pcid = 0");
        while ($row = $this->db->fetch_array($query)) {
            $pquery = $this->db->query("select * from " . table('news_cats') . " where pcid = " . $row['cid']);
            while ($prow = $this->db->fetch_array($pquery)) {
                $prow['addtime'] = _date($prow['addtime']);
                $row['pcats'][] = $prow;
            }
            $row['addtime'] = _date($row['addtime']);
            $cats[] = $row;
        }
        return $cats;
    }
    public function cats()
    {
        $this->assign('cats', $this->func_cats_list());
        $this->display();
    }
    public function cats_edit()
    {
        $cid = isset($_GET['cid']) ? $_GET['cid'] : false;
        if ($cid) {
            $row = $this->db->get_one("select * from " . table('news_cats') . " where cid = " . $cid);
            $this->assign('row', $row);
        }
        $this->assign('cats', $this->func_cats_top_list());
        $this->display();
    }
    public function func_cats_top_list()
    {
        $cats = $this->db->get_all("select * from " . table('news_cats') . " where pcid = 0");
        return $cats;
    }
    public function cats_save()
    {
        if ($_POST) {
            $setsql['pcid'] = P('pcid', '', '0');
            $setsql['name'] = P('name', '名称不能为空');
            $setsql['alias'] = P('alias');
            $setsql['px'] = P('px', '', '0');
            $setsql['status'] = P('status', '', '0');
            $setsql['addtime'] = time();
            $cid = P('cid', false);
            if ($cid) {
                $query = $this->db->update(table('news_cats'), $setsql, 'cid = ' . $cid);
            } else {
                $query = $this->db->add(table('news_cats'), $setsql);
            }
            if ($query) {
                right_json();
            } else {
                error_json('修改失败');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function cats_del()
    {
        $cid = P('id', '提交错误');
        $query = $this->db->delete(table('news_cats'), 'cid = ' . $cid);
        if ($query) {
            right_json();
        } else {
            error_json('删除失败');
        }
    }
}